package com.aizhixin.lab.course.course.service;

import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.course.course.domain.CourseChapterDomain;
import com.aizhixin.lab.course.course.domain.CourseChapterFileDomain;
import com.aizhixin.lab.course.course.entity.CourseChapter;
import com.aizhixin.lab.course.course.entity.CourseChapterFile;
import com.aizhixin.lab.course.course.entity.CourseChapterLike;
import com.aizhixin.lab.course.course.repository.CourseChapterFileRepository;
import com.aizhixin.lab.course.course.repository.CourseChapterLikeRepository;
import com.aizhixin.lab.course.course.repository.CourseChapterRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Slf4j
@Service
@Transactional
public class StudentChapterService {
    @Autowired
    private CourseChapterRepository chapterRepository;
    @Autowired
    private CourseChapterFileRepository chapterFileRepository;
    @Autowired
    private CourseChapterLikeRepository chapterLikeRepository;
    public CourseChapter getChapter(Long id) {
        CourseChapter chapter = chapterRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
        if (chapter != null) {
            chapter.setChildrens(null);
        }
        return chapter;
    }

    public List<CourseChapterDomain> getListWithoutHide(String courseId,Long createBy) {
        List<CourseChapter> list = chapterRepository.findByCourseIdAndDeleteFlagAndParentIdIsNullOrderByOrderNo(courseId, DataValidity.VALID.getState());
        List<CourseChapterDomain> result = setChapterDomain(list,createBy);
        return result;
    }

    private List<CourseChapterDomain> setChapterDomain(List<CourseChapter> list,Long createBy) {
        List<CourseChapterDomain> result = new ArrayList<>();
        if (!list.isEmpty()) {
            for (CourseChapter item : list) {
                if (item.getDeleteFlag().equals(DataValidity.VALID.getState()) && !item.getIsHide()) {
                    CourseChapterDomain d = new CourseChapterDomain();
                    BeanUtils.copyProperties(item, d);
                    CourseChapterLike courseChapterLike = chapterLikeRepository.findByChapterIdAndCreatedBy(item.getId(), createBy);
                    if (!Objects.isNull(courseChapterLike)){
                        d.setLike(true);
                    }
                    List<CourseChapterFile> files = chapterFileRepository.findByChapterIdAndDeleteFlagOrderByNo(item.getId(), DataValidity.VALID.getState());
                    if (!files.isEmpty()) {
                        Map<Integer, Integer> fileCountMap = new HashMap<>();
                        List<CourseChapterFileDomain> fileDomains = new ArrayList<>();
                        for (CourseChapterFile file : files) {
                            if (file.getDeleteFlag().equals(DataValidity.VALID.getState()) && !file.getIsHide()) {
                                CourseChapterFileDomain fd = new CourseChapterFileDomain();
                                BeanUtils.copyProperties(file, fd);
                                fileDomains.add(fd);
                                Integer fileCount = fileCountMap.get(fd.getType());
                                if (fileCount == null) {
                                    fileCount = 0;
                                }
                                fileCountMap.put(fd.getType(), fileCount + 1);
                            }
                        }
                        d.setFiles(fileDomains);
                        d.setFileCounts(fileCountMap);
                    }
                    if (item.getChildrens() != null && !item.getChildrens().isEmpty()) {
                        List<CourseChapterDomain> children = setChapterDomain(item.getChildrens(),createBy);
                        d.setChildrens(children);
                    }
                    result.add(d);
                }
            }
        }
        return result;
    }
}
